package com.example.lastwork2.fragments.recfragments;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.renderscript.Type;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.lastwork2.R;
import com.example.lastwork2.adapter.RecAdapter;
import com.example.lastwork2.fragments.detail.ShopDetailActivity;
import com.example.lastwork2.model.Shop;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.regex.Matcher;

public class RecRecFragment extends Fragment {

    private GridView gvShop;
    //商品数据源
    private List<Shop> shops;
    //适配器
    private RecAdapter recAdapter;


    //通过handler从子线程接收数据
    private Handler handler;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //绑定布局文件
        View recPage = inflater.inflate(R.layout.rec_rec_fragment,null);

        //绑定对应控件
        gvShop = (GridView)recPage.findViewById(R.id.recommend_shop);

        //初始化商品数据源
        createShop();

        //点击进入详情页
        gvShop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //实现页面的跳转
                Intent shopdetail = new Intent();
                shopdetail.setClass(
                        getActivity(),
                        ShopDetailActivity.class
                );
                //2.跳转的时候带着当前网格中的信息过去，根据position从shops中找到对应数据
                //并使用bubble进行封装
                Shop shop = shops.get(position);
                Bundle bundle  = new Bundle();
                bundle.putSerializable("shop", shop);

                //3.将bundle封装到intent中
                shopdetail.putExtra("obj",bundle);
                //4.启动跳转
                startActivity(shopdetail);


            }
        });


        return recPage;
    }




    //初始化商品
    private void createShop() {
        shops = new ArrayList<>();
        //获取数据库中的商品信息
        //1.向服务器发送请求，请求商品信息
        sendToServer();



    }

    private void sendToServer() {
        String url1 = "http://10.0.2.2:8080/bigwork/shops";
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    URL url = new URL(url1+"?category=recommend");
                    URLConnection urlConnection = url.openConnection();
                    //接收服务器发来的数据
                    InputStream inputStream = urlConnection.getInputStream();
                    //创建字符流读取数据
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
                    String str = bufferedReader.readLine();
                    //服务器发送json数据成功
                    System.out.println(str);
                    //关闭流
                    bufferedReader.close();
                    inputStream.close();
                    //解析数据
                    Gson gson = new Gson();
                    shops = gson.fromJson(str,new TypeToken<List<Shop>>(){}.getType());
                    System.out.println(shops.size());

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


//                在子线程里面绑定适配器
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //实例化适配器对象
                        recAdapter = new RecAdapter(
                                getActivity(),
                                R.layout.rec_rec_fragment_item,
                                shops
                        );
                        //绑定适配器
                        gvShop.setAdapter(recAdapter);
                    }
                });



            }
        }).start();



    }

}